package com.sasis.webapp;

/* 
 * Created on 2005-3-10 
 * To change the template for this generated file go to 
 * Window>Preferences>Java>Code Generation>Code and Comments 
 */
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
//import java.util.List;
import java.util.Map;

//import antlr.collections.List;

/**
 * @param args
 */

/**
 * @author ewen To change the template for this generated type comment go to
 *         Window>Preferences>Java>Code Generation>Code and Comments
 */


public class FileReadWrite {

	
	/**
	 * write file
	 * 
	 * @param filePath
	 * @param fileName
	 * @param args[]
	 * @throws IOException
	 */
	public void writeFile(String filePath, String fileName, String[] args)
			throws IOException {
		FileWriter fw = new FileWriter(filePath + fileName);
		PrintWriter out = new PrintWriter(fw);
		for (int i = 0; i < args.length; i++) {
			out.write(args[i]);
			out.println();
			out.flush();
		}
		fw.close();
		out.close();
	}

	/**
	 * write file
	 * 
	 * @param filePath
	 * @param fileName
	 * @param args
	 * @throws IOException
	 */
	public void writeFile(String filePath, String fileName, String args)
			throws IOException {
		FileWriter fw = new FileWriter(filePath + fileName);
		fw.write(args);
		fw.close();
	}

	/**
	 * create and delete file
	 * 
	 * @param filePath
	 * @param fileName
	 * @return if create return true
	 * @throws IOException
	 */
	public boolean createAndDeleteFile(String filePath, String fileName)
			throws IOException {
		boolean result = false;
		File file = new File(filePath, fileName);
		System.out.println(file.length());
		if (file.exists()) {
			file.delete();
			result = true;
			System.out.println("file had been deleted");
		} else {
			file.createNewFile();
			result = true;
			System.out.println("file had been created");
		}
		return result;
	}

	/**
	 * create file and delete dir
	 * 
	 * @param folderName
	 * @param filePath
	 * @return if deleted rturn true
	 */
	public boolean createAndDeleteFolder(String folderName, String filePath) {
		boolean result = false;
		try {
			File file = new File(filePath + folderName);
			if (file.exists()) {
				file.delete();
				System.out.println("dir alreay exist, had been delete");
				result = true;
			} else {
				file.mkdir();
				System.out.println("dir was not exist, had been created");
				result = true;
			}
		} catch (Exception ex) {
			result = false;
			System.out.println("CreateAndDeleteFolder is error:" + ex);
		}
		return result;
	}

	/**
	 * output the dir for all files and dir name
	 * 
	 * @param filePath
	 */
	public void readFolderByFile(String filePath) {
		File file = new File(filePath);
		File[] tempFile = file.listFiles();
		for (int i = 0; i < tempFile.length; i++) {
			if (tempFile[i].isFile()) {
				System.out.println("File : " + tempFile[i].getName());
			}
			if (tempFile[i].isDirectory()) {
				System.out.println("Directory : " + tempFile[i].getName());
			}
		}
	}

	/**
	 * check the file if empty
	 * 
	 * @param filePath
	 * @param fileName
	 * @return if null return true
	 * @throws IOException
	 */
	public boolean fileIsNull(String filePath, String fileName)
			throws IOException {
		boolean result = false;
		FileReader fr = new FileReader(filePath + fileName);
		if (fr.read() == -1) {
			result = true;
			System.out.println(fileName + "the file was no data");
		} else {
			System.out.println(fileName + "the file have data in here");
		}
		fr.close();
		return result;
	}

	/**
	 * read all file content
	 * 
	 * @param filePath
	 * @param fileName
	 * @throws IOException
	 */
	public void readAllFile(String filePath, String fileName)
			throws IOException {
		FileReader fr = new FileReader(filePath + fileName);
		int count = fr.read();
		while (count != -1) {
			System.out.print((char) count);
			count = fr.read();
			if (count == 13) {
				fr.skip(1);
			}
		}
		fr.close();
	}

	/**
	 * read file one line by one line
	 * 
	 * @param filePath
	 * @param fileName
	 * @throws IOException
	 */
	public void readLineFile(String filePath, String fileName)
			throws IOException {
		FileReader fr = new FileReader(filePath + fileName);
		BufferedReader br = new BufferedReader(fr);
		String line = br.readLine();
		while (line != null) {
			System.out.println(line);
			line = br.readLine();
		}
		br.close();
		fr.close();
	}

	public ArrayList readLineList(String fileName) throws IOException {
		FileReader fr = new FileReader(fileName);
		BufferedReader br = new BufferedReader(fr);
		String oneLine = br.readLine();
		ArrayList arrayList = new ArrayList();
		while (oneLine != null) {
			
			//System.out.println(oneLine);
			arrayList.add(oneLine);
			oneLine = br.readLine();
			
		}
		br.close();
		fr.close();
		return arrayList;
		
	}

	public static void main(String[] args) throws IOException {
		FileReadWrite f = new FileReadWrite();
		// System.out.println(f.readFile("f:\\LinkFile.java"));
		// f.readAllFile("c:\\", "test1.txt");
		//f.readLineFile("c:\\", "test1.txt");
		
		ArrayList readList=f.readLineList("c:/msdn.cvs");
		System.out.println(readList.size());
		if(readList.size()>0&&readList!=null){
			Map mapStr= new HashMap();
			for(int i=0;i<readList.size();i++){
				//System.out.println(readList.get(i));
				//String[] strs=new String[2];
				String[] strs = String.valueOf(readList.get(i)).split(",");
				if(mapStr.containsKey(strs[0]))System.out.println("Had a copy here!");
				mapStr.put(strs[0], strs[0]);
				//String lengthInt=strs[0];
				//System.out.println("cardNum:"+strs[0]+"---PhoneNum:"+strs[1]+"---Password:"+strs[2]);
				System.out.println("cardNum:"+strs[0]);
			}
				
		}
		
		// System.out.println(f.fileIsNull("f:\\","122.txt"));
		// f.readFolderByFile("F:\\PDF");
		// System.out.println(f.createAndDeleteFolder("ss","f:\\"));
		// System.out.println(f.createAndDeleteFile("f:\\ss\\","TestFile.dat"));
		
		//boolean isAction=f.createAndDeleteFile("c:\\ss\\", "test2.txt");
		//System.out.println(isAction);
		
	   // String[] ss = new String[10]; for (int i = 0; i < ss.length; i++) {
	    //ss[i] = "Information tech and internet test" + i; }
	   // f.writeFile("c:\\ss\\", "test2.txt", ss);
		
		
		 
	}

}
